package com.ee.chat.common.service.impl;

import com.ee.chat.common.dao.ChannelConfigMapper;
import com.ee.chat.common.dao.ChannelMapper;
import com.ee.chat.common.dao.ChannelShortUrlMapper;
import com.ee.chat.common.entity.Channel;
import com.ee.chat.common.entity.ChannelConfig;
import com.ee.chat.common.entity.ChannelShortUrl;
import com.ee.chat.common.serconfig.MyDwzConfig;
import com.ee.chat.common.service.IChannelShortUrlService;
import com.ndhutu.ee.util.dwz.suo.DwzSuoUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author AppiJ
 * @Date 2020/4/21 14:24
 */
@Service
public class ChannelShortUrlServiceImpl implements IChannelShortUrlService {

    @Resource
    ChannelShortUrlMapper shortUrlMapper;
    @Resource
    ChannelConfigMapper configMapper;
    @Resource
    ChannelMapper channelMapper;
    @Resource
    MyDwzConfig dwzConfig;

    @Override
    public void addChannelShortUrlByCid(Integer cid) throws Exception {
        List<Channel> channelList = channelMapper.queryChannelList();
        if(channelList != null && channelList.size() != 0){
            ChannelConfig config = configMapper.selectById(cid);
            shortUrlMapper.deleteByCid(cid);
            for(Channel channel : channelList){
                String longUrl = config.getUrl() + "?channelCode=" + channel.getCode();
                ChannelShortUrl channelShortUrl = new ChannelShortUrl(cid, channel.getId(), longUrl, DwzSuoUtil.createShortUrl(longUrl, dwzConfig));
                shortUrlMapper.insert(channelShortUrl);
            }
        }
    }

    @Override
    public void addChannelShortUrlByPid(Integer pid) throws Exception {
        List<ChannelConfig> configList = shortUrlMapper.queryNoShortUrlChannelByPid(pid);
        if(configList != null && configList.size() != 0){
            Channel channel = channelMapper.selectById(pid);
            for(ChannelConfig config : configList){
                String longUrl = config.getUrl() + "?channelCode=" + channel.getCode();
                ChannelShortUrl channelShortUrl = new ChannelShortUrl(config.getId(), pid, longUrl, DwzSuoUtil.createShortUrl(longUrl, dwzConfig));
                shortUrlMapper.insert(channelShortUrl);
            }
        }
    }
}
